package com.pro01.backend.controller;

import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.Forum2PostLikeService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/forum2/posts")
public class Forum2PostLikeController {

    @Autowired
    private Forum2PostLikeService likeService;

    // 点赞
    @PostMapping("/{postId}/like")
    public Result<Void> likePost(@PathVariable Long postId,
                                 HttpServletRequest request) {
        Long userId = TokenUtil.getUserId(request);
        try {
            int result = likeService.likePost(userId, postId);
            return result > 0 ? Result.success() : Result.error("点赞失败");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    // 取消点赞
    @DeleteMapping("/{postId}/like")
    public Result<Void> unlikePost(@PathVariable Long postId,
                                   HttpServletRequest request) {
        Long userId = TokenUtil.getUserId(request);
        try {
            int result = likeService.unlikePost(userId, postId);
            return result > 0 ? Result.success() : Result.error("取消点赞失败");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

}

